package com.yiwenup.leetcode.top_interview_150;

/**
 * 文件描述
 *
 * @Project struct-algorithm
 * @Package com.yiwenup.leetcode.top_interview_150
 * @Author yiwenup
 * @Date 2024-12-11 18:26:05
 * @Description https://leetcode.cn/problems/minimum-size-subarray-sum/submissions/585872670/?envType=study-plan-v2&envId=top-interview-150
 */
public class No0209 {
    public int minSubArrayLen(int target, int[] nums) {
        int sum = 0;
        int left = 0;
        int right = 0;
        int ans = Integer.MAX_VALUE;

        while(right < nums.length) {
            sum += nums[right];
            while(sum > target) {
                ans = Math.min(ans, right - left + 1);
                sum -= nums[left];
                left++;
            }
            if(sum == target) {
                ans = Math.min(ans, right - left + 1);
            }
            right++;
        }

        return ans == Integer.MAX_VALUE ? 0 : ans;
    }
}
